package com.zhuhjay.app.web.controller.system;

import com.zhuhjay.common.base.controller.BaseController;
import com.zhuhjay.common.web.model.vo.PageInfoVo;
import com.zhuhjay.service.system.model.dto.ConfigDto;
import com.zhuhjay.service.system.model.entity.SystemConfig;
import com.zhuhjay.service.system.service.SystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuHJay
 * @since 2023-08-30
 */
@Api(tags = "系统配置")
@Validated
@RestController
@RequestMapping("config")
public class SystemConfigController extends BaseController {

    @Resource
    private SystemConfigService configService;

    @ApiOperation("分页查询系统配置")
    @PreAuthorize("@perm.hasPerm('system:manager:config:list')")
    @GetMapping("page/{pageNum}/{pageSize}")
    public PageInfoVo<SystemConfig> page(
            @PathVariable @Range(min = 1, max = Integer.MAX_VALUE) Integer pageNum,
            @PathVariable @Range(min = 5, max = 30) Integer pageSize) {
        return configService.configsPage(pageNum, pageSize);
    }

    @ApiOperation("新增系统配置")
    @PreAuthorize("@perm.hasPerm('system:manager:config:add')")
    @PostMapping
    public void add(@RequestBody @Validated ConfigDto config) {
        configService.add(config);
    }

    @ApiOperation("修改系统配置")
    @PreAuthorize("@perm.hasPerm('system:manager:config:edit')")
    @PutMapping("{id}")
    public void edit(@PathVariable Integer id, @RequestBody @Validated ConfigDto config) {
        configService.edit(id, config);
    }

    @ApiOperation("删除系统配置")
    @PreAuthorize("@perm.hasPerm('system:manager:config:delete')")
    @DeleteMapping("{id}")
    public void delete(@PathVariable Integer id) {
        configService.deleteConfig(id);
    }

}
